<?php

class GuestController
{
    private static $controllerCache;
    
    public function callApi ($param) {
        if (empty($param)) return false;
        
        $route = array(
            'diaryShare'              =>  array('ShareController','diaryShare'),// 日记分享链接
            'topicShare'              =>  array('ShareController','topicShare'),// 话题分享链接
            'photoShare'              =>  array('ShareController','photoShare'),// 图片分享链接
            'doTask'                  =>  array('ShareController', 'doTask'),// 做任务
            'getDuiBaShareUrl'         => array('ShareController', 'getDuiBaShareUrl'),//获取兑吧分享链接
        
            'getHotDiary'             =>  array('StatisticsController','getHotDiary'),
            'getHotDiaryByGuest'      =>  array('StatisticsController','getHotDiary'),// 热门日记
            'getHotRecommendDiary'    =>  array('StatisticsController','getHotRecommendDiary'),// 热门推荐日记

            'getAnonymousCard'    =>  array('AnonymousController','cardAction'),// 匿名卡片详情
            'getAnonymousList'    =>  array('AnonymousController','anonymousListAction'),// 匿名卡片列表
            'getAnonymousComment'    =>  array('AnonymousController','commentAction'),// 匿名评论列表
            'getAnonymousNewComment'  =>  array('AnonymousController','commentNewAction'),// 匿名评论列表
            'getAnonymousRecentCard'  =>  array('AnonymousController','recentCardAction'),// 最新的一条匿名卡片详情

            'getTopicInfo'            =>  array('GroupController', 'getTopicDetail'),// 话题详情
            'getTopicList'            =>  array('GroupController', 'getTopicLists'),// 话题列表
            'getTopicCommentList'     =>  array('GroupController', 'getTopicCommentList'),// 话题评论列表

			'getCardList'             =>  array('ShopController', 'getCardList'),// 会员列表
            'getSkinList'             =>  array('ShopController', 'getSkinList'),// 皮肤列表
            'getSkinDetail'           =>  array('ShopController', 'getSkinDetail'),// 皮肤详情
            'getProSkinList'          =>  array('ShopController', 'getProSkinList'),// 专业版皮肤列表
            'getProSkinDetail'        =>  array('ShopController', 'getProSkinDetail'),// 专业版皮肤详情
            'getShopBanner'           =>  array('ShopController', 'getShopBanner'),// 商店banner

            'getSquareListByGuest'    =>  array('HotSquareController','getSquareList'),// 最新日记
            'getDiaryDetailByGuest'   =>  array('DiaryController', 'getDiaryDetailByGuest'),// 日记详情
            'getDuiBaAutoLoginUrl'    =>  array('UserController','getDuiBaAutoLoginUrl'),//兑吧登陆链接
	        
            'getDiaryWithComments'    =>  array('DiaryController', 'getDiaryWithComments'),// 点滴详情
            'getDiarysByTopic'        =>  array('DiaryController', 'getDiarysByTopic'),// 根据话题查看点滴列表
            'getDiaryTopicInfo'       =>  array('DiaryController', 'getDiaryTopicInfo'),// 获取点滴话题详情
            'videoPlay'               =>  array('DiaryController', 'videoPlay'),// 视频点击计数
            'subscriptionReward'      =>  array('DiaryController', 'subscriptionReward'),// 订阅号打赏页面

	        	
            'getPlannerNewStickersList'=> array('ShopController','getInterfaceInfo'),//新版贴纸列表
            'getPlannerNewBrushsList'  => array('ShopController','getInterfaceInfo'),//新版贴纸列表
            'getPlannerNewTagsList'    => array('ShopController','getInterfaceInfo'),//新版贴纸列表
            'getPlannerNewFramesList'  => array('ShopController','getInterfaceInfo'),//分类边框
            'getPlannerNewPluginsList'  => array('ShopController','getInterfaceInfo'),//分类插件
            'getPlannerBootmgrsList'  => array('ShopController','getInterfaceInfo'),//分类选择器

            'getResourcesById'         => array('ShopController','getResourcesById'),
        	
            'getPlannerTagsList'	  =>  array('ShopController','getInterfaceInfo'),//标签列表
            'getPlannerPapersList'	  =>  array('ShopController','getInterfaceInfo'),//背景列表
            'getPlannerStickersList'  =>  array('ShopController','getInterfaceInfo'),//贴纸列表
            'getPlannerBrushsList'	  =>  array('ShopController','getInterfaceInfo'),//花边笔列表
            'getPlannerTempletsList'  =>  array('ShopController','getInterfaceInfo'),//模板列表
        	'getHighligherList'	      =>  array('ShopController','getHighligherList'),
            'buyPlannerTags'		   => array('ShopController','getInterfaceInfo'),
            'buyPlannerPapers'		   => array('ShopController','getInterfaceInfo'),
            'buyPlannerStickers'	   => array('ShopController','getInterfaceInfo'),
            'buyPlannerBrushs'		   => array('ShopController','getInterfaceInfo'),         
            'buyPlannerTemplets'	   => array('ShopController','getInterfaceInfo'),
            'buyPlannerFrames'	       => array('ShopController','getInterfaceInfo'),
            'buyPlannerPlugins'        => array('ShopController','getInterfaceInfo'),
            'buyFont'		  		   => array('ShopController','buyFont'),
            'getShopDisguise'          => array('ShopController','getShopDisguise'),
			'getDeskList'			   => array('ShopController','getDeskList'),
            'robotIndex'               => ['RobotController', 'index'],
            'clientLog'                => ['RpcController', 'clientLog'],
            'getUserRole'              => ['UserController', 'getUserRole'],
            'setUserRole'              => ['UserController', 'setUserRole'],
            'getArticleInfo'           => ['ArticleController', 'getArticleInfo'],
            'getUserArticleList'           => ['ArticleController', 'getUserArticleList'],
            'getArticleComments'           => ['ArticleController', 'getArticleComments'],
        );
        
        // check action
        $action = $param['action'];
        if (!isset($route[$action])){
            return FALSE;
        }
        
        $guest_controller   =   $route[$action][0];
        $guest_route        =   $route[$action][1];
        $controller         =   $this->newController($guest_controller);
        $info          	    =   $controller->$guest_route($param);
        $this->clearError();
        
        return  $info;
    }
    
    private function clearError () {
        if (in_array(AC::$errorNo, array('DC008', 'SA001', 'GRC002', 'DC004', 'DCC007', 'DCP006'))) {
            return false;
        }
        
        AC::$errorMsg = '';
        AC::$errorNo = null;
    }
    
    private function newController($controller)
    {
        if (empty($controller))
        {
            AC::$errorNo    =   'SA001';
            return  FALSE;
        }
        if (isset(self::$controllerCache[$controller])  &&  is_resource(self::$controllerCache[$controller]))
        {
            return  self::$controllerCache[$controller];
        }
        self::$controllerCache[$controller] =   new $controller();
        return  self::$controllerCache[$controller];
    }
}
